<?php

namespace App\Http\Controllers\Wxpay;

use Illuminate\Http\Request;

ini_set('date.timezone','Asia/Shanghai');

require_once "WxPayData.php";
require_once "WxPayNativePay.php";
require_once "phpqrcode.php";

use WxPayUnifiedOrder;
use NativePay;
use QRcode;
use App\Http\Controllers\UsersController as users;
use App\Order;
use App\Product;
use App\Rad_users;

class WxPayController
{

    //   微信扫码支付
    public function getWxpayCode($body=false,$attach=false,$out_trade_no=false,$total_fee=false,$Goods_tag=false,$product_id=false)
    {
        $total_fee  = 1;
        $input = new WxPayUnifiedOrder();
        $notify = new NativePay();

        $input->SetBody($body);                                                             //   主体
        $input->SetAttach($attach);                                                         //  标题
        $input->SetOut_trade_no($out_trade_no);                                             //  订单号
        $input->SetTotal_fee($total_fee);                                                   //  价格
        $input->SetTime_start(date("YmdHis"));                                       //  开始时间
        $input->SetTime_expire(date("YmdHis", time() + 600));             //  结束时间
        $input->SetGoods_tag($Goods_tag);                                                   //  商品标签
        $input->SetNotify_url("http://api.niumoip.com/v1/user/wechatpaycallback");          //  回调地址
//        $input->SetNotify_url("http://interface.sanrenyun.com/pc/v1/wechatpaycallback");    //  回调地址
        $input->SetTrade_type("NATIVE");                                              //   支付类型
        $input->SetProduct_id($product_id);                                                 //  产品id

        $result = $notify->GetPayUrl($input);
        return $result["code_url"];

//        return QRcode::png($result["code_url"]);
    }


    /**
     * @param Request $request
     * @return string
     * 检测微信扫码支付订单状态
     */
    public function CheckWxPayOrderStatus(Request $request)
    {
        if (empty($request->input('token')) || !$request->has('token'))
        {
            return 'error';
        }

        if (empty($request->input('product')) || !$request->has('product'))
        {
            return 'error';
        }

        $token = $request->input('token');

        $product_id = $request->input('product');

        $user_id = users::getIdByWebToken($token);
        if(!$user_id)   return 'error';

        $products = Product::find($product_id);
        if($products == NULL) return 'error';

        $order = Order::where('user_id',$user_id)->where('product_id',$product_id)->where('pay_channel','2')->orderBy('created_at','desc')->first();

        if($order->status == 0)
        {
            $return = array(
                'code' => -1,
                'msg' => "False",
            );
            return response()->json($return);

        }else{
            $return = array(
                'code' => 0,
                'msg' => "SUCCESS",
            );
            return response()->json($return);
        }
    }
    
    
    



    static function fwrite($data){

        $file=storage_path('logs/wxpay.log');
        $myFile = fopen($file, "a+") or die("Unable to open file!");
    //    $data = json_encode($data);
        $txt = $data."\n";
        fwrite($myFile, $txt);
        fclose($myFile);
    }

}
